package com.schooleducation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonComment;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.LessonContentInfo;
import com.schooleducation.entity.LessonReply;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;
import com.schooleducation.uitls.DateJsonValueProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/lessonCenter")
public class LessonCenterController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	/**
	 * 获取课程内容页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String getPage(@PathVariable(value = "id") Integer id, HttpSession session, Model model, String content) {
		System.out.println(content);
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:../login";
		} else {
			LessonSubscribe ls = new LessonSubscribe();
			ls.setUser(user);
			Lesson lesson = serviceFac.getLessonService().getLessonById(id);
			ls.setLesson(lesson);
			if (serviceFac.getLessonService().getIsSubscribeLessonPass(ls)) {
				List<LessonContent> list = serviceFac.getLessonService().getLessonContentList(id);
				if (list.size() == 0) {

					return "error/noData";
				} else {
					LessonContentInfo lcis;
					LessonContent lc;
					List<LessonComment> lcs;
					if(content == null) {
						lc = list.get(0);
						lcis = serviceFac.getLessonService().getLessonContentInfo(lc.getId());
						lcs = serviceFac.getLessonCommentService().getCommentAndReplyList(lc.getId(), 0);
						
					} else {
						int contentId = Integer.valueOf(content);
						lc = this.serviceFac.getLessonService().getLessonContentById(contentId);
						lcis = serviceFac.getLessonService().getLessonContentInfo(contentId);
						lcs = serviceFac.getLessonCommentService().getCommentAndReplyList(contentId, 0);
					}
					
					model.addAttribute("user", user);
					model.addAttribute("lesson", lesson);
					model.addAttribute("lessonContent",lc);
					model.addAttribute("contentList", list);
					model.addAttribute("contentInfo", lcis);
					model.addAttribute("commentList", lcs);

					return "lessonCenter";
				}
			} else {
				return "redirect:../lessonInfo/" + id;
			}
		}
	}

	/**
	 * 获取课程详细内容
	 * 
	 * @param contentId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getContentInfo(Integer contentId, int infoId) {
		JSONObject jsonObject;
		JsonConfig config = new JsonConfig();
		if (infoId == 0) {
			// 获取课题详细内容
			LessonContentInfo info = serviceFac.getLessonService().getLessonContentInfo(contentId);
			config.setExcludes(new String[] { "lessonContent" });
			jsonObject = JSONObject.fromObject(info, config);
		} else {
			jsonObject = JSONObject.fromObject(new LessonContentInfo(), config);
		}
		config.setExcludes(
				new String[] { "lessonContent", "comment", "userInfo", "userRoles", "userNum", "userPassword" });
		config.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
		// 获取留言和回复列表
		jsonObject.put("comment", JSONArray
				.fromObject(serviceFac.getLessonCommentService().getCommentAndReplyList(contentId, 0), config));

		return jsonObject.toString();
	}

	/**
	 * 发表留言
	 * 
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "comment", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String publishComment(HttpSession session, String data) {
		Map<String, Object> state = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			LessonComment lc = (LessonComment) JSONObject.toBean(JSONObject.fromObject(data), LessonComment.class);
			lc.setUser(user);
			if (serviceFac.getLessonCommentService().addLessonComment(lc)) {
				state.put("commentId", lc.getId());
				state.put("save", "success");
			} else {
				state.put("save", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}

	/**
	 * 发布回复
	 * 
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "reply", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String publishReply(HttpSession session, String data) {
		Map<String, Object> state = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			LessonReply lr = (LessonReply) JSONObject.toBean(JSONObject.fromObject(data), LessonReply.class);
			lr.setUser(user);
			if (serviceFac.getLessonCommentService().addLessonReply(lr)) {
				state.put("save", "success");
			} else {
				state.put("save", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}

	/**
	 * 留言翻页
	 * 
	 * @param session
	 * @param contentId
	 * @return
	 */
	@RequestMapping(value = "commentNext", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getCommentNext(HttpSession session, int contentId, int start) {

		List<LessonComment> lessonComments = serviceFac.getLessonCommentService().getCommentAndReplyList(contentId,
				start);
		JsonConfig config = new JsonConfig();
		config.setExcludes(
				new String[] { "lessonContent", "comment", "userInfo", "userRoles", "userNum", "userPassword" });
		config.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
		
		return JSONArray.fromObject(lessonComments, config).toString();
	}

}
